package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import com.jt.vo.PageResult;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    public SysResult login(@RequestBody User user){
        String token = userService.login(user);
        if(token ==null){
            return SysResult.fail();
        }
        return SysResult.success(token);
    }

    /**
     * 分页
     */
    @GetMapping("/list")
    public SysResult findUserList(PageResult pageResult){
        pageResult = userService.findUserList(pageResult);
        return SysResult.success(pageResult);
    }

    /**
     * 状态修改
     */
    @PutMapping("/status/{id}/{status}")
    public SysResult updateStatus(User user){

        userService.updateStatus(user);
        return SysResult.success();
    }
    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public SysResult deleteUserById(@PathVariable Integer id){
        userService.deleteUserById(id);
        return SysResult.success();
    }
    /**
     * 用户新增
     */
    @PostMapping("/addUser")
    public SysResult saveUser(@RequestBody User user){
        userService.saveUser(user);
        return SysResult.success();
    }
    /**
     * 用户修改--数据回显
     */
    @GetMapping("/{id}")
    public SysResult findUserById(@PathVariable Integer id){
        User user=userService.findUserById(id);
        return SysResult.success(user);
    }
    /**
     * 用户修改--数据提交
     */
    @PutMapping("/updateUser")
    public SysResult updateUser(@RequestBody User user){
        userService.updateUser(user);
        return SysResult.success();
    }

}
